package com.zyp.dao.imp;

import com.zyp.bean.Student;
import com.zyp.dao.StudentDao;
import com.zyp.ubil.DriverUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class StudentDaoImp implements StudentDao {

    QueryRunner qr = null;

    //增加
    @Override
    public int add(Object[] objects) {
        Connection con = null;
        qr = new QueryRunner();
        //1.获取数据库连接
        try {
            //获取数据库连接
            con = DriverUtil.getConnection();
            //sql插入语句
            String sql = "INSERT INTO student VALUES(?,?,?,?,?,?,?);";
            int update = qr.update(con, sql, objects);//通过
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭
            DriverUtil.close(con);
        }
        return 0;
    }

    //删除
    @Override
    public int delete(String stirng) {
        qr = new QueryRunner(DriverUtil.getDataSource());
        String sql = "delete from student where studentID=? ";
        try {
            int update = qr.update(sql, stirng);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //更新
    @Override
    public int update(Object[] objects) {
        qr = new QueryRunner(DriverUtil.getDataSource());
        String sql = "select * from student where studentID=?";
        //存储不同的值
        Object[] storage = new Object[8];
        try {
            //sql查询数据库中是否存在要修改的id
            Student employee = qr.query(sql, new BeanHandler<Student>(Student.class), objects[1]);
            System.out.println(employee);
            //判断获取过来的值是否为空
            if (employee == null) {
                return 0;
            } else {
                //将获取到的值存储到数组中
                storage[0] = employee.getID();
                storage[1] = employee.getStudentID();
                storage[2] = employee.getStudentName();
                storage[3] = employee.getGender();
                storage[4] = employee.getBirthday();
                storage[5] = employee.getMailbox();
                storage[6] = employee.getRemarks();
                for (int i = 1; i < objects.length; i++) {
                    if (!objects[i].equals("") && !storage[i].equals(objects[i]) && !storage[i].equals("--")) {
                        storage[i] = objects[i];
                    }
                }
                storage[7] = objects[1];

                for (Object o : storage) {
                    System.out.println(o);
                }
                sql = "update student set id=?,studentID=?,studentName=?,gender=?,birthday=?,mailbox=?,remarks=? where studentID=?";
                int update = qr.update(sql, storage);
                System.out.println(update);
                return update;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //分页查询
    @Override
    public List<Object[]> pagingQuery(int i) {
        qr = new QueryRunner(DriverUtil.getDataSource());
        String sql = "select * from student limit ?,5";
        try {
            List<Object[]> query = qr.query(sql, new ArrayListHandler(), i);
            if (query != null) {
                return query;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
